﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Xml.Linq;
using System.Data.SqlClient;
using System.Threading;

namespace GMA_RSS
{
    public partial class Form1 : Form
    {

        string m_connectionString = @"Data Source=SQL01;Initial Catalog=SuperSign;Persist Security Info=True;User ID=sa; Password=sa";
        string m_rss_Feed8 = @"http://e.wtx.me/blackberry2011/ssn.asp";
        bool m_StartFetch = true;

        public Form1()
        {
            InitializeComponent();
        }

        private void Bt_Start_Click(object sender, EventArgs e)
        {
            Bt_Start.Enabled = false;
            ThreadPool.QueueUserWorkItem(new WaitCallback(GetXmlFormat));
        }
        protected void GetXmlFormat(object o)
        {
            while (m_StartFetch)
            {
                XDocument x8Doc = new XDocument();
                try
                {
                    x8Doc = XDocument.Load(m_rss_Feed8);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    string result8 = ProcessFeed(x8Doc);

                    SaveIntoDB(result8);
                }
                Thread.Sleep(300000);
            }
        }

        protected string ProcessFeed(XDocument xDoc)
        {
            string fontColor = @"[Color Val=""#FFFFFF""]";
            string fontColorAuth = @"[Color Val=""#949494""]";

            string imageTag = @"[IMAGETAG ID=""544""]";

            string rssFeed = "";

            IEnumerable<XElement> feedElements;
            feedElements = xDoc.Descendants("MSG");

            foreach (XElement el in feedElements)
            {

                XElement TxtElement = el.Descendants("TXT").First();
                XElement AuthElement = el.Descendants("AUTHOR").First();

                string largeSpace = "     ";
                string smallSpace = "  ";

                string tweetTxt = imageTag + smallSpace + fontColorAuth + AuthElement.Value + smallSpace + fontColor + TxtElement.Value + largeSpace;
                rssFeed += tweetTxt;
            }

            if (rssFeed == "")
                rssFeed = @"Welcome To TimeSquare";

            return rssFeed;
        }

        protected void SaveIntoDB(string rss8Feed)
        {
            //string update7RSSFeed = @"Update DataTag set LastValue=@LASTVALUE where [Name] = '[DominoRSS7]'";


            string update8RSSFeed = @"Update DataTag set LastValue='" + rss8Feed.Replace("'", "''") + "' where [Name] = '[BlackBerryTweet]'";


            ExecuteQuery(update8RSSFeed);
            /*
            SqlCommand rss8Command = new SqlCommand();
            rss8Command.CommandText = update8RSSFeed;
            rss8Command.CommandType = CommandType.Text;
            rss8Command.Connection = con;

            SqlCommand rss9Command = new SqlCommand();
            rss9Command.CommandText = update9RSSFeed;
            rss9Command.CommandType = CommandType.Text;
            rss9Command.Connection = con;

            SqlParameter lastValue = new SqlParameter();
            lastValue.ParameterName = @"LASTVALUE";
            lastValue.SqlDbType = SqlDbType.VarChar;
            */
            /*
            //RSS7
            lastValue.Size = rss7Feed.Length;
            lastValue.Direction = ParameterDirection.Input;
            lastValue.Value = rss7Feed;
            ExecuteQuery(rss7Command);*/

            /*/RSS8
            lastValue.Size = rss8Feed.Length;
            lastValue.Direction = ParameterDirection.Input;
            lastValue.Value = rss8Feed;
            ExecuteQuery(rss8Command);

            //RSS9
            lastValue.Size = rss8Feed.Length;
            lastValue.Direction = ParameterDirection.Input;
            lastValue.Value = rss8Feed;
            ExecuteQuery(rss9Command);
             * */
        }

        private void ExecuteQuery(string command)
        {
            // Try to open the database and execute the update.
            SqlConnection con = new SqlConnection(m_connectionString);
            SqlCommand cmd = new SqlCommand(command, con);

            // Try to open the database and execute the update.
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception err)
            {
            }
            finally
            {
                con.Close();
            }
        }

        private void Bt_Stop_Click(object sender, EventArgs e)
        {
            m_StartFetch = false;
            Bt_Start.Enabled = true;
        }

    }
}
